package com.nami.conver.domain.handler.lock;


import java.util.concurrent.locks.ReentrantLock;

public class StoreKeyLock {

    /**
     * 锁数量 可调
     * 128
     * 256
     * 512
     */
    private static final int LOCK_SIZE = 256;

    private static final ReentrantLock[] lock = new ReentrantLock[LOCK_SIZE];

    static {
        for (int i = 0; i < LOCK_SIZE; i++) {
            lock[i] = new ReentrantLock();
        }
    }

    private static int hash(String key) {
        return Math.abs(key.hashCode()) % LOCK_SIZE;
    }

    public static void lock(String key) {
        lock[hash(key)].lock();
    }

    public static void unlock(String key) {
        lock[hash(key)].unlock();
    }


}
